package views;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JRadioButtonMenuItem;

import controllers.MainController;

public class MainWindowMenuBar extends JMenuBar
{
	/**
	 * Generated serial ID
	 */
	private static final long	serialVersionUID	= -4025354356433501094L;
	
	private JMenu 					actionsMenu			= null;
	private JMenu					attractorTypeMenu	= null;
	private JMenu					colorMenu			= null;
	
	private JMenuItem				calcAttractorItem	= null;
	private JCheckBoxMenuItem		pauseCalcItem		= null;
	private JMenuItem				exitItem			= null;
	private JCheckBoxMenuItem		showDebugItem		= null;
	
	private JRadioButtonMenuItem	pickoverItem		= null;
	private JRadioButtonMenuItem	dejongItem			= null;
	private JRadioButtonMenuItem	customItem			= null;
	
	private JRadioButtonMenuItem	bwItem				= null;
	private JRadioButtonMenuItem	colorItem			= null;
	
	private MainController 			mainController		= null;

	public MainWindowMenuBar(MainController mainController)
	{
		this.mainController = mainController;
		
		JPopupMenu.setDefaultLightWeightPopupEnabled(false);
		
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		
		setName("MenuBar");
		
		add(getActionsMenu());
		add(getAttractorTypeMenu());
		add(getColorMenu());
	}
	
	private JMenu getColorMenu() {
		if (colorMenu == null)
		{
			colorMenu = new JMenu("Color Modes");
			
			ButtonGroup colorTypeGroup = new ButtonGroup();
			
			colorTypeGroup.add(getColorItem());
			colorTypeGroup.add(getBwItem());
			
			colorMenu.add(getColorItem());
			colorMenu.add(getBwItem());
		}
		return colorMenu;
	}

	private JMenu getActionsMenu()
	{
		if (actionsMenu == null)
		{
			actionsMenu = new JMenu("Actions");
			
			actionsMenu.add(getCalcAttractorItem());
			actionsMenu.add(getPauseCalcItem());
			actionsMenu.addSeparator();
			actionsMenu.add(getShowDebugItem());
			actionsMenu.addSeparator();
			actionsMenu.add(getExitItem());
		}
		
		return actionsMenu;
	}
	
	private JMenu getAttractorTypeMenu()
	{
		if (attractorTypeMenu == null)
		{
			attractorTypeMenu = new JMenu("Attractor type");
			
			ButtonGroup attractorTypeGroup = new ButtonGroup();
			
			attractorTypeGroup.add(getPickoverItem());
			attractorTypeGroup.add(getDejongItem());
			attractorTypeGroup.add(getCustomItem());
			
			attractorTypeMenu.add(getPickoverItem());
			attractorTypeMenu.add(getDejongItem());
			attractorTypeMenu.addSeparator();
			attractorTypeMenu.add(getCustomItem());
		}
		return attractorTypeMenu;
	}

	private JMenuItem getCalcAttractorItem()
	{
		if (calcAttractorItem == null)
		{
			calcAttractorItem = new JMenuItem("Calc and Draw Attractor");
			calcAttractorItem.setActionCommand("CALC");
			calcAttractorItem.addActionListener(mainController);
		}
		
		return calcAttractorItem;
	}

	private JCheckBoxMenuItem getPauseCalcItem()
	{
		if (pauseCalcItem == null)
		{
			pauseCalcItem = new JCheckBoxMenuItem("Pause Calculations", false);
			pauseCalcItem.setActionCommand("PAUSE");
			pauseCalcItem.addActionListener(mainController);
		}
		
		return pauseCalcItem;
	}

	private JMenuItem getExitItem()
	{
		if (exitItem == null)
		{
			exitItem = new JMenuItem("Exit");
			exitItem.setActionCommand("EXIT");
			exitItem.addActionListener(mainController);
		}
		
		return exitItem;
	}
	
	private JCheckBoxMenuItem getShowDebugItem()
	{
		if (showDebugItem == null)
		{
			showDebugItem = new JCheckBoxMenuItem("Show debug data");
			showDebugItem.setActionCommand("SHOW_DEBUG");
			showDebugItem.addActionListener(mainController);
		}
		
		return showDebugItem;
	}
	
	private JRadioButtonMenuItem getPickoverItem()
	{
		if (pickoverItem == null)
		{
			pickoverItem = new JRadioButtonMenuItem("Pickover", true);
			pickoverItem.setActionCommand("PICKOVER");
			pickoverItem.addActionListener(mainController);
		}
		return pickoverItem;
	}
	
	private JRadioButtonMenuItem getDejongItem()
	{
		if (dejongItem == null)
		{
			dejongItem = new JRadioButtonMenuItem("De Jong");
			dejongItem.setActionCommand("DEJONG");
			dejongItem.addActionListener(mainController);
		}
		return dejongItem;
	}
	
	private JRadioButtonMenuItem getCustomItem() 
	{
		if (customItem == null)
		{
			customItem = new JRadioButtonMenuItem("Custom Attractor");
			customItem.setActionCommand("CUSTOM");
			customItem.addActionListener(mainController);
		}
		return customItem;
	}
	
	private JRadioButtonMenuItem getBwItem() 
	{
		if (bwItem == null)
		{
			bwItem = new JRadioButtonMenuItem("Black&White");
			bwItem.setActionCommand("BW");
			bwItem.addActionListener(mainController);
		}
		return bwItem;
	}

	private JRadioButtonMenuItem getColorItem() 
	{
		if (colorItem == null)
		{
			colorItem = new JRadioButtonMenuItem("Color", true);
			colorItem.setActionCommand("COLOR");
			colorItem.addActionListener(mainController);
		}
		return colorItem;	
	}
}
